package com.leyou.collect.service;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.client.item.ItemClient;
import com.leyou.collect.pojo.Collect;
import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.pojo.Sku;
import org.apache.lucene.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 登录后的购物车业务
 */
@Service
public class CollectService {
    //收藏商品格式： < userId,  Map<skuId,Cart对象的json字符串>  >
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ItemClient itemClient;

    //收藏商品
    public void addCollect(Map<String,Object> paraMap) {
        Long skuId = Long.valueOf (paraMap.get("id")+"");
        Integer spuId = (Integer) paraMap.get("spuId");
        String subTitle = (String) paraMap.get("subTitle");
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //2.获取商品的参数
        List<Long> list = new ArrayList<>();
        list.add(skuId);
        List<Sku> skus = itemClient.findSkusBySkuIds(list);
        Collect collect = new Collect();
        if (CollectionUtils.isEmpty(skus)){
            throw new LyException(500,"收藏商品不存在");
        }
        collect.setSkuId(skuId);
        collect.setImage(skus.get(0).getImages());
        collect.setPrice(skus.get(0).getPrice());
        collect.setOwnSpec(skus.get(0).getOwnSpec());
        collect.setTitle(skus.get(0).getTitle());
        collect.setSubTitle(subTitle);
        collect.setSpuId(spuId);

        //3.判断商品是否已存在
        if (boundHashOps.hasKey(skuId.toString())){
            throw new LyException(500,"该商品已经添加");
        }

        //4 收藏商品
        boundHashOps.put(skuId.toString(),JsonUtils.toString(collect));
    }






    /**
     * 获取当前登录用户的购物车
     * @return
     */
    public BoundHashOperations<String, Object, Object> getLoginUserCart () {
        //1. 获取当前登录用户
        UserInfo loginUser = UserHolder.getUser();
        //取出用户ID
        String userId = loginUser.getId().toString();

        //2. 绑定当前登录用户的购物车
        return redisTemplate.boundHashOps(userId);
    }

    //判断是否被收藏
    public Boolean checkIsCollect(Long skuId) {
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();
        //3.判断商品是否已存在
        if (boundHashOps.hasKey(skuId.toString())){
            return true;
        }else {
            return false;
        }
    }

    //取消收藏
    public void cancelCollect(Long skuId) {
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //2.删除其中指定的商品
        boundHashOps.delete(skuId.toString());
    }

    //查询所有收藏的商品
    public List<Collect> findAllCollects() {
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //2.1获取当前商品价格(前台没有修改价格的按钮，为了方便演示)
        List<Long> skuIds = boundHashOps.keys().stream().map(spuId->Long.valueOf(spuId.toString())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(skuIds)){
            return null;
        }
        List<Sku> skus = itemClient.findSkusBySkuIds(skuIds);


        //2.2获取购物车的所有Cart对象
        //values(): 取出hash中的value值
        List<Collect> collects = boundHashOps
                .values()
                .stream()
                .map(collect->JsonUtils.toBean(collect.toString(),Collect.class))
                .collect(Collectors.toList());


        //2.3更新最新价格
        for (Collect collect : collects) {
            for (Sku sku : skus) {
                if (collect.getSkuId().equals(sku.getId())&&!collect.getPrice().equals(sku.getPrice())){
                    collect.setCurrentPrice(sku.getPrice());
                }

            }
        }

        return collects;
    }

    public List<Long> findAllCollects2() {
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //2.1获取当前商品价格(前台没有修改价格的按钮，为了方便演示)
        List<Long> skuIds = boundHashOps.keys().stream().map(spuId->Long.valueOf(spuId.toString())).collect(Collectors.toList());

        return skuIds;
    }
}

